package com.block.datapush.form.item;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.form.item.util.BaiNiaoUtil;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BaiNiaoPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getPushLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {

            String queryUrl = formInfo.getQueryUrl();
            String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("PhoneMd5", mobileMd5);

            log.info("【佰鸟】撞库开始【queryUrl】{}【bizParam】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(queryUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【佰鸟】撞库结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            Integer code = resultObject.getInteger("code");
            String message = resultObject.getString("message");

            if (ObjectUtil.equal(code, Integer.valueOf(1))){
                    return true;
            }
            return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {
        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);

            String requestUrl = formInfo.getRequestUrl();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("PhoneNumber", memberExtInfo.getMobile());
            paramMap.put("Source", formInfo.getApiKey());
            paramMap.put("RealName", memberExtInfo.getUsername());
            paramMap.put("IdNumber", memberExtInfo.getIdCard());
            paramMap.put("Age", IdcardUtil.getAgeByIdCard(memberExtInfo.getIdCard()));
            paramMap.put("Sexy", IdcardUtil.getGenderByIdCard(memberExtInfo.getIdCard()));
            paramMap.put("City", StrUtil.fillAfter(memberExtInfo.getCityCode(), '0', 6));

            paramMap.put("Creditlimit", 0);
            /**
             * 无：0
             * 600以下：1
             * 600-650:2
             * 650-700:3
             * 700以上：4
             */
            paramMap.put("SesameScore", BaiNiaoUtil.getZhima(memberExtInfo.getZhimaScore()));
            paramMap.put("House", BaiNiaoUtil.getHouse(memberExtInfo.getHouse()));
            paramMap.put("Car", BaiNiaoUtil.getCar(memberExtInfo.getCar()));
            paramMap.put("Policy", BaiNiaoUtil.getBaodan(memberExtInfo.getInsurance()));
            paramMap.put("Social", BaiNiaoUtil.getShebao(memberExtInfo.getSocialInsurance()));
            paramMap.put("License", false);
            paramMap.put("IncomeMounth", "8000");
            paramMap.put("Wages", true);//是否打卡工资
            paramMap.put("GJJ", BaiNiaoUtil.getGjj(memberExtInfo.getGjj()));
            paramMap.put("Overdue", BaiNiaoUtil.getYuqi(memberExtInfo.getYuqi()));
            paramMap.put("JDScoreLimit", BaiNiaoUtil.getBaitiao(memberExtInfo.getBaitiao()));
            paramMap.put("AliLoan", BaiNiaoUtil.getHuabei(memberExtInfo.getHuabei()));

            log.info("【佰鸟】导单开始【requestUrl】{}【bizParam】{}【paramMap】{}", requestUrl, JSON.toJSONString(paramMap), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【佰鸟】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);

            Integer code = resultObject.getInteger("code");
            String msg = resultObject.getString("message");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(code));
            pushResultDTO.setMessage(msg);
            pushResultDTO.setParamContent(JSON.toJSONString(paramMap));
            if (ObjectUtil.equal(code, Integer.valueOf(1))){
                pushResultDTO.setSuccess(true);
                pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
            }else{
                pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;

    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }
}
